package com.google.code.dweibo.core.topic;

import org.apache.log4j.Logger;

import weibo4j.Comments;
import weibo4j.Timeline;
import weibo4j.http.ImageItem;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;

import com.google.code.dweibo.core.auth.LoginService;
import com.google.code.dweibo.core.auth.LoginType;
import com.google.code.dweibo.util.IOUtil;
import com.google.code.dweibo.util.WeiboServiceFactory;

public class SinaTopicActionService {
	static Logger log = Logger.getLogger(SinaTopicActionService.class);
	private LoginService loginService;
	
	public SinaTopicActionService() throws Exception{
		loginService = WeiboServiceFactory.getLoginService(LoginType.SINA);
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
	}
	
	public void updateStatus(String statuses) throws Exception{
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
	
		Timeline tm = new Timeline();
		try {
			Status status = tm.UpdateStatus(statuses);
			log.info(status.toString());
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}
	
	public void updateStatus(String statuses,String imagePath) throws Exception{
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
	
		 Timeline tm = new Timeline();
		 
	     byte[] contents = IOUtil.getByteArray(imagePath);
	     
	     ImageItem item = new ImageItem(contents);
	     
	     Status status = tm.UploadStatus(statuses,item);
	     
	     log.info(status.toString());
		
	}

	
	public void deleteCommentById(String cid) throws Exception {
		// TODO Auto-generated method stub
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
		Comments.destroyComment(cid);
	}

	
	public void deleteStatusById(String id) throws Exception {
		// TODO Auto-generated method stub
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
		Timeline tm = new Timeline();
		try {
			Status status = tm.Destroy(id);
			log.info(status.toString());
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	
	public void replyComment(String cid, String id,String comment)
			throws Exception {
		// TODO Auto-generated method stub
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
		Comments.replyComment(cid, id, comment);
	}

	
	public void sendComment(String id,String comment) throws Exception {
		// TODO Auto-generated method stub
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
		Comments.createComment(comment, id);
	}

	
	public void rePostStatus(String id,String content,int needComment)
			throws Exception {
		// TODO Auto-generated method stub
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
		Timeline tm = new Timeline();
		
		Status status = tm.Repost(id, content, needComment);
		log.info(status.toString());
	}
	
	public void favoriteStatus(String id) throws Exception{
		
	}
	
	


}
